package tt.dz.egobus.servicemanager.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tt.dz.egobus.api.page.Filter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Anson
 * @date 17-4-19
 * @name 管理分页工具类
 * @since 1.0.4
 */
public final class DubboManagerPagableUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(DubboManagerPagableUtils.class);

    private static final String SPLIT = "#";

    private static final Class<Filter> clazz = Filter.class;

    private static final Class<?>[] clazzes = new Class<?>[]{String.class, Object.class};


    /**
     * @param params key = eq#anson   value= object
     * @author Anson
     * @date 17-4-19
     * @name 将当前参数中过滤查询转换为分页的filter
     * @since 1.0.4
     */
    public final List<Filter> adaptcher(Map<String, Object> params) {

        List<Filter> filters = new ArrayList<Filter>();

        if (params == null) {
            return filters;
        }
        Filter filter = null;
        Method method = null;
        String[] keys = null;
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            keys = this.split(entry.getKey());
            if (keys == null || keys.length != 2) {
                continue;
            }
            method = this.getMethod(keys[0]);
            if (method == null) {
                continue;
            }
            filter = this.invoker(method, keys[1], entry.getValue());
            if (filter == null) {
                continue;
            }
            filters.add(filter);
        }

        return filters;
    }

    /**
     * @param op
     * @author Anson
     * @date 17-4-19
     * @name 查找Filter的方法
     * @since 1.0.4
     */
    private final Method getMethod(String op) {
        try {
            Method method = clazz.getMethod(op, clazzes);
            if (method != null) {
                int modify = method.getModifiers();
                if (Modifier.isPublic(modify) || Modifier.isStatic(modify)) {
                    return method;
                }
            }
            return null;
        } catch (NoSuchMethodException e) {
            LOGGER.error("处理参数，封装filter时候，获取对应的静态方法未找到，op={}", op, e);
            return null;
        }
    }

    private String[] split(String key) {
        if (StringUtils.isEmpty(key)) {
            return new String[0];
        }
        String[] strings = key.split(SPLIT);
        return strings;
    }

    private Filter invoker(Method method, String opProperty, Object object) {
        if (method == null) {
            return null;
        }
        try {
            Object obj = method.invoke(null, opProperty, object);
            if (obj != null && clazz.isAssignableFrom(obj.getClass())) {
                return (Filter) obj;
            } else {
                return null;
            }
        } catch (IllegalAccessException e) {
            LOGGER.error("处理参数，调用对应的静态方法权限异常，method={},op={}", method, opProperty, e);
            return null;
        } catch (InvocationTargetException e) {
            LOGGER.error("处理参数，调用对应的静态方法执行异常，method={},op={}", method, opProperty, e);
            return null;
        }

    }

}
